package com.android.bookgarden.utli.otherlogin;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zouqh on 2018/4/20.
 */

public class QQLogin {
    private static final String APP_ID = "1105602574";//官方获取的APPID
    private Tencent mTencent;
    private Context context;
    private String TAG ="QQLogin";
    private UserInfo mUserInfo;
    private BaseUiListener baseUiListener;
    private QqCallback qqCallback;
    public QQLogin(Context context, QqCallback qqCallback){
        this.context = context;
        this.qqCallback = qqCallback;
        mTencent = Tencent.createInstance(APP_ID, context);
    }

    public void setmTencent(){
        baseUiListener = new BaseUiListener();
        this.qqCallback.getUILister(baseUiListener);
        mTencent.login((Activity) context, "all", baseUiListener);
    }


    /**
     * 自定义监听器实现IUiListener接口后，需要实现的3个方法
     * onComplete完成 onError错误 onCancel取消
     */
    public class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object response) {
            Toast.makeText(context, "授权成功", Toast.LENGTH_SHORT).show();
            Log.e(TAG, "response:" + response);
            JSONObject obj = (JSONObject) response;
            try {
                String openID = obj.getString("openid");
                String accessToken = obj.getString("access_token");
                String expires = obj.getString("expires_in");
                mTencent.setOpenId(openID);
                mTencent.setAccessToken(accessToken, expires);
                QQToken qqToken = mTencent.getQQToken();
                mUserInfo = new UserInfo(context, qqToken);
                mUserInfo.getUserInfo(new IUiListener() {
                    @Override
                    public void onComplete(Object response) {
                        //是一个json串response.tostring，直接使用gson解析就好
                        Log.e(TAG, "登录成功" + response.toString());
                        //登录成功后进行Gson解析即可获得你需要的QQ头像和昵称
                        // Nickname  昵称
                        //Figureurl_qq_1 //头像
                    }

                    @Override
                    public void onError(UiError uiError) {
                        Log.e(TAG, "登录失败" + uiError.toString());
                    }

                    @Override
                    public void onCancel() {
                        Log.e(TAG, "登录取消");

                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(UiError uiError) {
            Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCancel() {
            Toast.makeText(context, "授权取消", Toast.LENGTH_SHORT).show();

        }

    }

    public interface QqCallback{
        void getUILister(BaseUiListener base);
        void getQQInfo(String json);
    }
}
